<template>
  <div class="my-body">
    <el-row>
      <el-col :span="24">
        <div class="my-table-search">
          <el-form ref="form" :model="form">
            <el-row :gutter="10">
              <el-col :span="3">
                <el-input v-model="input" placeholder="请输入内容" />
              </el-col>
              <el-col :span="3">
                <el-select v-model="region" placeholder="请选择部门">
                  <el-option
                    v-for="item in options"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                    :disabled="item.disabled"
                  >
                  </el-option>
                </el-select>
              </el-col>

              <el-col :span="3">
                <el-button type="primary" @click="rukumingxi = true"
                plain >查询</el-button
                >
              </el-col>
            </el-row>
          </el-form>
        </div>
      </el-col>
    </el-row>

    <div style="width: 100%; float: left; ">
      <div style="width: 100%; float: left">
        <div>
          <!-- <el-button type="warning">删除人员</el-button> -->
        </div>
        <el-dialog
          title="添加人员"
          :visible.sync="rukumingxi"
          width="600px"
          :before-close="handleClose"
        >
          <div style="width: 100%">
            <el-form ref="form" :model="form" label-width="80px">
              <!-- <el-form-item label="人员编号">
                <el-input
                  v-model="form.nameid"
                  style="width: 300px; height: 50px; float: left"
                ></el-input>
                <el-button type="success">申请</el-button>
              </el-form-item> -->
              <el-form-item label="用户ID" class="usr">
                <el-input v-model="form.username"></el-input>
              </el-form-item>
              <el-form-item label="口令" class="usr">
                <el-input v-model="form.password"></el-input>
              </el-form-item>
              <el-form-item label="重复口令" class="usr">
                <el-input v-model="form.uepassword"></el-input>
              </el-form-item>

              <el-form-item label="部门">
                <el-select
                  v-model="form.department_id"
                  placeholder="请选择部门"
                >
                  <el-option
                    v-for="item in options"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                    :disabled="item.disabled"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="角色">
                <el-select
                  v-model="form.admin_group_id"
                  placeholder="请选择销售区域"
                >
                  <el-option
                    v-for="item in jueselist"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                    :disabled="item.disabled"
                    @click.native="handleSelectTeam(item)"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="销售区域">
                <el-select v-model="form.area_id" placeholder="请选择销售区域">
                  <el-option
                    v-for="item in roles"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                    :disabled="item.disabled"
                    @click.native="handleSelectTeam(item)"
                  >
                  </el-option>
                </el-select>
              </el-form-item>

              <el-form-item label="职位">
                <el-select v-model="form.position_id" placeholder="请选择职位">
                  <el-option
                    v-for="item in zhiwei"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                    :disabled="item.disabled"
                    @click.native="handlezhiwei(item)"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="隶属于">
                <!-- <el-select
                  v-model="form.department_id"
                  placeholder="请选择部门"
                  style="width: 200px; height: 50px; float: left"
                >
                  <el-option
                    v-for="item in renyuanzhiwei"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                    :disabled="item.disabled"
                  >
                  </el-option>
                </el-select> -->
                <el-select
                  v-model="form.pid"
                  placeholder="请选择人员"
                  style="
                    width: 200px;
                    height: 50px;
                    float: left;
                    margin-left: 20px;
                  "
                >
                  <el-option
                    v-for="item in renyuanlist"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                    :disabled="item.disabled"
                  >
                  </el-option>
                </el-select>
              </el-form-item>

              <el-form-item label="姓名">
                <el-input
                  v-model="form.name"
                  style="width: 220px; height: 50px; float: left"
                ></el-input>
              </el-form-item>
              <el-form-item label="性别">
                <el-select v-model="form.sex" placeholder="请选择性别">
                  <el-option
                    v-for="(item, index) in sexlist"
                    :key="index"
                    :label="item.title"
                    :value="index"
                    :disabled="item.disabled"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="出生日期">
                <el-date-picker
                  v-model="form.birthday"
                  type="date"
                  placeholder="选择日期"
                  format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd"
                >
                </el-date-picker>
              </el-form-item>
              <!-- <el-form-item label="身份信息">
                <el-input
                  v-model="form.name"
                  style="width: 220px; height: 50px; float: left"
                ></el-input>
              </el-form-item> -->
              <el-form-item label="联系方式">
                <el-input
                  v-model="form.mobile"
                  style="width: 220px; height: 50px; float: left"
                ></el-input>
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="form.email"
                  style="width: 220px; height: 50px; float: left"
                ></el-input>
              </el-form-item>
              <el-form-item label="备注:">
                <el-input type="textarea" v-model="form.remark"></el-input>
              </el-form-item>
            </el-form>
            <div style="margin-top: 30px">
              <el-button type="primary" @click="xinzeng()">添加</el-button>
              <el-button type="warning" @click="quxiao()">放弃</el-button>
            </div>
          </div>
        </el-dialog>

        <el-row>
          <el-col :span="24">
            <div class="my-table-tools">
              <div class="my-table-tools-left">
                <el-button
                  type="primary"
                  size="small"
                  icon="el-icon-plus"
                  plain
                  @click="handleAddRole"
                  >新增</el-button
                >
                <!-- <el-button
                  type="danger"
                  size="small"
                  icon="el-icon-delete"
                  plain
                  >删除</el-button
                > -->
              </div>
              <div class="my-table-tools-right">
                <el-button
                  icon="el-icon-refresh"
                  circle
                  plain
                  @click="refresh"
                />
              </div>
            </div>
          </el-col>
          <el-col :span="24">
            <div class="my-table-body">
              <el-table
                border
                style="width: 100%"
               :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
                :data="tableData"
                height="600"
                max-height="700"
              >
                <!-- <el-table-column type="selection" width="55"> </el-table-column> -->
                <el-table-column fixed prop="id" label="人员编号" width="155">
                </el-table-column>

                <el-table-column prop="name" label="人员名称" width="155">
                </el-table-column>
                <el-table-column prop="sex" label="性别" width="155">
                  <template slot-scope="scope">
                    <div v-if="scope.row.sex == 0">女</div>
                    <div v-else>男</div>
                  </template>
                </el-table-column>
                <el-table-column prop="email" label="邮件" width="155">
                </el-table-column>
                <el-table-column prop="mobile" label="人员联系方式" width="155">
                </el-table-column>
                <el-table-column prop="birthday" label="出生日期" width="155">
                </el-table-column>
                <el-table-column prop="group_name" label="角色" width="155">
                </el-table-column>
                <!-- <el-table-column prop="name" label="添加用户" width="155">
            </el-table-column> -->
                <el-table-column
                  prop="create_time"
                  label="添加时间"
                  width="155"
                >
                </el-table-column>
                <el-table-column prop="login_name" label="用户账号" width="155">
                </el-table-column>

                <!-- <el-table-column prop="password" label="用户密码" width="155">
                </el-table-column> -->
                <!-- <el-table-column prop="name" label="部门编号" width="155">
            </el-table-column> -->
                <el-table-column
                  prop="department_name"
                  label="部门"
                  width="155"
                >
                </el-table-column>
                <el-table-column
                  prop="position_name"
                  label="职位名称"
                  width="155"
                >
                </el-table-column>
                <el-table-column prop="area_name" label="销售区域" width="155">
                </el-table-column>

                <el-table-column prop="remark" label="备注" width="155">
                </el-table-column>
                <el-table-column label="操作" width="200" fixed="right">
                  <template slot-scope="scope">
                    <!-- <el-button
              size="mini"
               type="text"
              @click="handleEdit(scope.row)"
            >
              <i class="el-icon-edit"></i>编辑</el-button
            > -->
  
                    <el-button
                      size="mini"
                      type="text"
                      @click="handleDelete(scope.row)"
                      class="my-btn-text-color-2"
                      ><i class="el-icon-delete"></i>删除</el-button
                    >
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <div class="my-table-pagination">
              <el-pagination
                background
                :current-page="currentPage"
                :page-sizes="[100,200,300,400]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="totallist"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
    <el-dialog
      title="编辑人员"
      :visible.sync="dialogVisiblebj"
      width="600px"
      :before-close="handleClose"
    >
      <div style="width: 100%">
        <el-form ref="form" :model="form" label-width="80px">
          <!-- <el-form-item label="人员编号">
                <el-input
                  v-model="form.nameid"
                  style="width: 300px; height: 50px; float: left"
                ></el-input>
                <el-button type="success">申请</el-button>
              </el-form-item> -->
          <el-form-item label="用户ID" class="usr">
            <el-input v-model="forms.username"></el-input>
          </el-form-item>
          <el-form-item label="口令" class="usr">
            <el-input v-model="forms.password"></el-input>
          </el-form-item>
          <el-form-item label="重复口令" class="usr">
            <el-input v-model="forms.uepassword"></el-input>
          </el-form-item>

          <el-form-item label="部门">
            <el-select v-model="forms.department_id" placeholder="请选择部门">
              <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.name"
                :value="item.id"
                :disabled="item.disabled"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="角色">
            <el-select
              v-model="forms.admin_group_id"
              placeholder="请选择销售区域"
            >
              <el-option
                v-for="item in jueselist"
                :key="item.id"
                :label="item.name"
                :value="item.id"
                :disabled="item.disabled"
                @click.native="handleSelectTeam(item)"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="销售区域">
            <el-select v-model="forms.area_id" placeholder="请选择销售区域">
              <el-option
                v-for="item in roles"
                :key="item.id"
                :label="item.name"
                :value="item.id"
                :disabled="item.disabled"
                @click.native="handleSelectTeam(item)"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="职位">
            <el-select v-model="forms.position_id" placeholder="请选择职位">
              <el-option
                v-for="item in zhiwei"
                :key="item.id"
                :label="item.name"
                :value="item.id"
                :disabled="item.disabled"
                @click.native="handlezhiwei(item)"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="隶属于">
            <el-select
              v-model="forms.department_id"
              placeholder="请选择部门"
              style="width: 200px; height: 50px; float: left"
            >
              <el-option
                v-for="item in renyuanzhiwei"
                :key="item.id"
                :label="item.name"
                :value="item.id"
                :disabled="item.disabled"
              >
              </el-option>
            </el-select>
            <el-select
              v-model="forms.pid"
              placeholder="请选择人员"
              style="width: 200px; height: 50px; float: left; margin-left: 20px"
            >
              <el-option
                v-for="item in renyuanlist"
                :key="item.id"
                :label="item.name"
                :value="item.id"
                :disabled="item.disabled"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="姓名">
            <el-input
              v-model="forms.name"
              style="width: 220px; height: 50px; float: left"
            ></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <el-select v-model="forms.sex" placeholder="请选择性别">
              <el-option
                v-for="(item, index) in sexlist"
                :key="index"
                :label="item"
                :value="index"
                :disabled="item.disabled"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="出生日期">
            <el-date-picker
              v-model="forms.birthday"
              type="date"
              placeholder="选择日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>
          <!-- <el-form-item label="身份信息">
                <el-input
                  v-model="form.name"
                  style="width: 220px; height: 50px; float: left"
                ></el-input>
              </el-form-item> -->
          <el-form-item label="联系方式">
            <el-input
              v-model="forms.mobile"
              style="width: 220px; height: 50px; float: left"
            ></el-input>
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input
              v-model="forms.email"
              style="width: 220px; height: 50px; float: left"
            ></el-input>
          </el-form-item>
          <el-form-item label="备注:">
            <el-input type="textarea" v-model="forms.remark"></el-input>
          </el-form-item>
        </el-form>
        <div style="margin-top: 30px">
          <el-button type="primary" @click="xinzeng()">添加</el-button>
          <el-button type="warning">放弃</el-button>
        </div>
      </div>
    </el-dialog>
    <!-- <div style="width: 100%; height: 400px; float: left; margin-top: 50px">
      <div style="width: 100%; float: left">
        <div>
          <el-button type="success" @click="bangdin = true">绑定用户</el-button>
          <el-button type="warning">解绑用户</el-button>
        </div>
        <el-dialog
          title="添加人员"
          :visible.sync="bangdin"
          width="30%"
          :before-close="handleClose"
        >
          <div style="width: 100%">
            <el-form ref="form" :model="form" label-width="80px">
              
            </el-form>
            <div style="margin-top: 30px">
              <el-button type="primary">添加</el-button>
              <el-button type="warning">放弃</el-button>
            </div>
          </div>
        </el-dialog>
        <template>
          <el-table :data="tableData" style="width: 100%">
            <el-table-column type="selection" width="55"> </el-table-column>
            <el-table-column fixed prop="bianhao" label="用户账号">
            </el-table-column>

            <el-table-column prop="name" label="用户密码"> </el-table-column>
            <el-table-column prop="name" label="部门编号"> </el-table-column>
            <el-table-column prop="name" label="部门"> </el-table-column>
            <el-table-column prop="name" label="职位名称"> </el-table-column>
            <el-table-column prop="name" label="销售区域"> </el-table-column>
            <el-table-column prop="name" label="最后登陆时间">
            </el-table-column>
            <el-table-column prop="name" label="备注"> </el-table-column>
          </el-table>
        </template>
      </div>
    </div> -->
  </div>
</template>

<script>
import { getData, postData } from "@/utils/request.js";
export default {
  data() {
    return {
      key: 1, // 为了能每次切换权限的时候重新初始化指令
      valuesj: 2024,
      dialogVisiblexiolie: false,
      diaxuanzhe: false,
      rukusaomiao: false,
      laokuan: false,
      rukumingxi: false,
      dialogVisiblebj: false,
      bangdin: false,
      region: "",
      totallist: "",
      options: [
        {
          value: "1",
          label: "全部",
        },
        {
          value: "2",
          label: "已签收",
        },
        {
          value: "3",
          label: "未签收",
        },
      ],
      data: [
        {
          label: "中国大陆",
          children: [
            {
              label: "上海",
            },
            {
              label: "北京",
            },
            {
              label: "四川",
            },
          ],
        },
      ],
      value1: "",
      defaultProps: {
        children: "children",
        label: "label",
      },
      value: "",
      chanpingguige: false,
      dialogVisible: false,
      dialogVisiblea: false,
      hospital: false,
      area: false,
      currentPage: 1,
      pageSize: 100,
      jinxiaoshang: false,
      shouhuor: false,
      diquxuanzhe: false,
      tableData: [],
      form: {
        name: "",
        username: "",
        password: "",
        uepassword: "",
        department_id: "",
        sex: "",
        birthday: "",
        pid: "",
        admin_group_id: "",
        mobile: "",
        position_id: "",
        area_id: "",
        email: "",
        remark: "",
      },
      forms: {
        name: "",
        username: "",
        password: "",
        uepassword: "",
        department_id: "",
        sex: "",
        birthday: "",
        pid: "",
        admin_group_id: "",
        mobile: "",
        position_id: "",
        area_id: "",
        email: "",
        remark: "",
      },
      sizeForm: {
        name: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
      },
      jueselist: [],
      options: [],
      roles: [],
      zhiwei: [],
      sexlist: [],
      renyuanlist: [],
      renyuanzhiwei: [],
      input: "",
      loading: false,
    };
  },
  created() {
    this.bianjicanshu();
    this.yonghulist();
  },
  methods: {
    handleAddRole(){
         this.rukumingxi = true
    },
    refresh() {
      const t = this;
      t.loading = true;
      setTimeout(function () {
        t.loading = false;
      }, 2000);
    },
    quxiao() {
      this.rukumingxi = false;
      this.$message({
        message: "已取消!",
      });
    },
    handleDelete(row) {
      // 删除操作
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var data = {
            ids: row.id,
            token: localStorage.getItem("token"),
          };
          postData("admin/del", data).then((res) => {
            // 处理响应数据
            if (res.code == 0) {
              console.log(res);
              this.yonghulist();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            } else {
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleEdit(row) {
      this.forms.name = row.name;
      this.forms.username = row.login_name;
      this.forms.password = row.password;
      this.forms.department_id = row.department_id;
      this.forms.sex = row.sex;
      this.forms.birthday = row.birthday;
      this.forms.pid = row.pid;
      this.forms.admin_group_id = row.admin_group_id;
      this.forms.mobile = row.mobile;
      this.forms.area_id = row.area_id;
      this.forms.position_id = row.position_id;
      this.forms.email = row.email;
      this.forms.remark = row.remark;
      // 编辑操作
      this.dialogVisiblebj = true;
    },
    handleSelectTeam(e) {
      console.log(e);
      this.form.pid = "";
      console.log(this.form.area_id);
    },
    handlezhiwei(e) {
      this.form.pid = "";

      var data = {
        area_id: this.form.area_id,
        position_id: this.form.position_id,
        token: localStorage.getItem("token"),
      };
      postData("admin/getTopUser", data).then((res) => {
        // 处理响应数据
        if (res.code == 0) {
          console.log(res, 666666666);
          this.renyuanlist = res.data;
        } else {
        }
      });
      var data = {
        position_id: this.form.position_id,
        token: localStorage.getItem("token"),
      };
      postData("admin/getTopPosition", data).then((res) => {
        // 处理响应数据
        if (res.code == 0) {
          console.log(res, 666666666);
          this.renyuanzhiwei = res.data;
        } else {
        }
      });
    },
    bianjicanshu() {
      var data = {
        token: localStorage.getItem("token"),
      };
      postData("admin/getParams", data).then((res) => {
        // 处理响应数据
        if (res.code == 0) {
          this.options = res.data.department;
          this.roles = res.data.admin_areas;
          this.zhiwei = res.data.position;
          this.sexlist = res.data.sex;
          this.jueselist = res.data.group;
        } else {
        }
      });
    },

    yonghulist() {
      var data = {
        page: this.currentPage,
        limit: this.pageSize,
        token: localStorage.getItem("token"),
      };
      postData("admin/indexlist", data).then((res) => {
        // 处理响应数据
        if (res.code == 0) {
          this.tableData = res.data.data;
          console.log(this.tableData);
          this.totallist = res.data.total;
        } else {
        }
      });
    },
    onSubmit() {
      console.log("submit!");
    },
    handleClose(done) {
      done();
      // this.$confirm('确认关闭？')
      // 	.then(_ => {
      // 		done();
      // 	})
      // 	.catch(_ => {});
    },
    xinzeng() {
      var data = {
        login_name: this.form.username,
        password: this.form.password,
        department_id: this.form.department_id,
        sex: this.form.sex,
        birthday: this.form.birthday,
        pid: this.form.pid,
        admin_group_id: this.form.admin_group_id,
        mobile: this.form.mobile,
        name: this.form.name,
        area_id: this.form.area_id,
        position_id: this.form.position_id,
        email: this.form.email,
        remark: this.form.remark,
        token: localStorage.getItem("token"),
      };
      postData("admin/operation", data).then((res) => {
        // 处理响应数据
        if (res.code == 0) {
          console.log(res);
          this.tableData = res.data.data;
          this.yonghulist();
          this.$message({
            message: "添加成功",
            type: "success",
          });
        } else {
          this.$message.error("错了哦，这是一条错误消息");
        }
      });
    },
    handleClick(row) {
      console.log(row);
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.yonghulist();
    },
    handleCurrentChange(val) {
      console.log(val);
      this.currentPage = val;
      this.yonghulist();
    },
  },
};
</script>

<style>
.usr {
  width: 250px;
}
</style>